import pandas as pd
import Core.Gadget as Gadget
import Core.MongoDB as MongoDB
import datetime
import math
import numpy as np
a=np.array(([1,2,3,4]))
np.median(a)#中位数
np.percentile(a,95)#95%分位数

def CYQ(symbol, datetime1, datetime2, scale = 100):
    distribution = []
    for i in range(scale+1):
        distribution.append(0)

    quotes = database.find("Quote", symbol + "_Time_86400_Bar", datetime1, datetime2)
    #quote = quotes[:100]
    unchanged_shares = quotes[0]["Values"]["FreeFloatShares"]
    FreeFloatShares = unchanged_shares
    data = []
    prices = []
    for i in range(len(quotes)):
        prices.append(quotes[i]["Close"])
    df = pd.DataFrame(prices, columns=['closeprice'])
    Max = df.max()
    Max = list(Max)
    Max = round(Max[0],2)
    Min = df.min()
    Min = list(Min)
    Min = round(Min[0],2)
    step = round((Max-Min)/scale,2)
    chip = []
    steps = Min
    for i in range(scale+1):
        chip.append(round(steps,2))
        steps += step
    debug = 1

    for i in range(len(quotes)):
        if unchanged_shares > 0:
            unchanged_shares -= quotes[i]["Volume"]
        if unchanged_shares < 0:
            unchanged_shares = 0
        if unchanged_shares > 0:
            order_number = math.floor((quotes[i]["Close"]-Min)/step)-1
            distribution[order_number] = quotes[i]["Volume"]
        elif unchanged_shares == 0:
            turn_shares = quotes[i]["Volume"]

            while turn_shares > 0:
                holders = 0
                holder_list = []
                for l in range(len(distribution)):
                    if distribution[l] > 0:
                        holders += 1
                        holder_list.append(l)
                seller_unit = turn_shares / holders
                turn_shares = 0
                for l in range(len(holder_list)):
                    distribution[l] -= seller_unit
                    if distribution[l] < 0:
                        distribution[l] = 0
                        turn_shares -= distribution[l]
            order_number = math.floor((quotes[i]["Close"] - Min) / step)-1
            distribution[order_number] = quotes[i]["Volume"]
    for i in range(len(chip)):
        total = sum(distribution)
        pass
        print(chip[i], round(distribution[i]), round(distribution[i]*100/total),"%")

    #for i in range(len(distribution)):
        #print(distribution[i])
    if unchanged_shares >0:
        print("注：股票并未全部换手，换手率为",unchanged_shares/FreeFloatShares)

def bottemup():
    j = 0
    order_number = math.floor((quotes[i]["Close"] - Min) / step)
    distribution[order_number] = quotes[i]["Volume"]
    while turn_shares > 0 and j < scale:
        turn_shares -= distribution[j]
        distribution[j] = 0
        if turn_shares > 0:
            j += 1
        elif turn_shares < 0:
            distribution[j] -= turn_shares
            break
        elif turn_shares == 0:
            break


database = MongoDB.MongoDB("10.13.38.31", "27017")
datetime1 = datetime.datetime(2017, 12, 1)
datetime1 = Gadget.ToUTCDateTime(datetime1)
datetime2 = datetime.datetime(2018, 10, 18)
datetime2 = Gadget.ToUTCDateTime(datetime2)

CYQ("000001.SZ", datetime1, datetime2, 50)